package com.spider.lwlk.mysql.task

import com.spider.lwlk.mongodb.task.taskstatus.TaskStatus
import groovy.sql.BatchingPreparedStatementWrapper
import groovy.sql.Sql

class ExtendTask extends BaseTask {

    private static final BookListRegex = /<ul class="list_aa listimg"[\s\S]*?<\/ul>/

    private static final BookRegex = /"(http:\/\/product\.dangdang\.com\/\d+\.html)#.*?"/

    private static final PagerRegex =/<ul class="paging"[\s\S]*?<\/ul>/

    private static final PageRegex = /"(\/pg\d+-[.0-9a-factory]*-srsort_pubdate_desc\.html)"/

    private books = [], pages = []

    @Override def onSuccess() {

        {   resp, reader ->

            taskStatus = TaskStatus.RequestSuccess

            def html = reader.text

            books = getBooks(html.find(BookListRegex))

            pages = getPages(html.find(PagerRegex))

            doUpdate()
        }
    }

    private def getBooks(def html) {

        html.eachMatch(BookRegex) { m -> books += m[1] }

        books.unique().sort()
    }

    private def getPages(def html) {

        html.eachMatch(PageRegex) { m -> pages += m[1] }

        pages = pages.collect() { page -> if(page[0] == '/') 'http://category.dangdang.com' + page else page  }

        pages.unique().sort()
    }

    @Override def onFilter() {

        {   Sql db ->

            books = books.findAll() { book -> !db.firstRow("select * from book_url_check_table where url = ?", [book]) }

            pages = pages.findAll() { page -> !db.firstRow("select * from page_url_check_table where url = ?", [page]) }
        }
    }

    @Override def onUpdate() {

        {   Sql db ->

            db.withBatch("insert into book_url_table(url) values(?)") { BatchingPreparedStatementWrapper ps ->

                books.each { url -> ps.addBatch([url]) }
            }

            db.withBatch("insert into page_url_table(url) values(?)") { BatchingPreparedStatementWrapper ps ->

                pages.each { url -> ps.addBatch([url]) }
            }

            taskStatus = TaskStatus.UpdateSuccess
        }
    }

    @Override def onCompleted() {

        {   Sql db ->

            db.execute("delete from page_url_table where url = ?", [path])

            db.executeInsert("insert into page_url_check_table(url) values(?)", [path])

            taskStatus = TaskStatus.Success
        }
    }
}